using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.IO;

namespace BRozklad
{


    public partial class Form1 : Form
    {

        private ProbaWork pw;
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'rozkladDataSet2.GetCurParas' table. You can move, or remove it, as needed.
            this.getCurParasTableAdapter1.Fill(this.rozkladDataSet2.GetCurParas);
            // TODO: This line of code loads data into the 'rozkladDataSet1.GetCurParas' table. You can move, or remove it, as needed.
            this.getCurParasTableAdapter.ClearBeforeFill = true;
            this.getCurParasTableAdapter.Fill(this.rozkladDataSet1.GetCurParas);
            pw = new ProbaWork();
        }

        private void MasStartWork()
        {
            for (int i = 0; i < 10; i++)
            {
                Configuration.result = null;
                Configuration.resultsize = 0;
                Configuration.iii = 0;

                pw.Go();
            }
        }
        private bool nextwork = false;
        private void StartWork()
        {
            Configuration.result = null;
            Configuration.resultsize = 0;
            Configuration.costforgoodres = Convert.ToInt32(textBox1.Text);


            if (nextwork == false)
            {
                pw.Go();
            }
            if (nextwork == true)
                pw.Nexts();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Configuration.dowork[0] = true;
            Configuration.iii = 0;
            Thread th = new Thread(new ThreadStart(StartWork));
            th.Start();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            Random rand = new Random();
            int r = rand.Next(255);
            int g = rand.Next(255);
            int b = rand.Next(255);
            Graphics gr = this.panel1.CreateGraphics();
            Color col = Color.FromArgb(r, g, b);
            SolidBrush myBrush = new SolidBrush(col);
            gr.FillEllipse(myBrush, new Rectangle(0, 0, 200, 300));
            if (Configuration.pos > Configuration.maxpos)
                Configuration.maxpos = Configuration.pos * 2;
            progressBar1.Maximum = Configuration.maxpos;                
            progressBar1.Value = Configuration.pos;
            label1.Text = Configuration.pos.ToString();
            label2.Text = Configuration.maxpos.ToString();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            lock (pw)
            {
                pw.WriteToFile();
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            Thread th = new Thread(new ThreadStart(MasStartWork));
            th.Start();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            lock (Configuration.dowork)
            {
                Configuration.dowork[0] = false;
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            Configuration.dowork[0] = true;
            nextwork = true;
            Thread th = new Thread(new ThreadStart(StartWork));
            th.Start();
        }

        private void timer1_Tick_1(object sender, EventArgs e)
        {
            Random rand = new Random();
            int r = rand.Next(255);
            int g = rand.Next(255);
            int b = rand.Next(255);
            Graphics gr = this.panel1.CreateGraphics();
            Color col = Color.FromArgb(r, g, b);
            SolidBrush myBrush = new SolidBrush(col);
            gr.FillEllipse(myBrush, new Rectangle(0, 0, 200, 300));
            if (Configuration.pos > Configuration.maxpos)
                Configuration.maxpos = Configuration.pos * 2;
            progressBar1.Maximum = Configuration.maxpos;
            progressBar1.Value = Configuration.pos;
            label1.Text = Configuration.pos.ToString();
            label2.Text = Configuration.maxpos.ToString();
            label3.Text = Configuration.goodcost.ToString();
            label4.Text = Configuration.mediancost.ToString();
        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            this.button2_Click(sender,e);
        }

        private void timer2_Tick_1(object sender, EventArgs e)
        {
            if (checkBox1.Checked)
            {
                lock (Configuration.canfill)
                {
                    if (Configuration.canfill[0] == true)
                        this.getCurParasTableAdapter1.Fill(this.rozkladDataSet2.GetCurParas);
                }
            }
        }



    }

}